package com.jd.system.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.system.domain.Dictionarydetail;
import com.jd.system.query.DictionarydetailQuery;
import com.jd.system.service.IDictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dictionarydetail")
public class DictionarydetailController {

    @Autowired
    private IDictionarydetailService dictionarydetailService;


    // 查询id
    @GetMapping("/{id}")
    public Dictionarydetail findOne(@PathVariable("id")Long id){
        return dictionarydetailService.loadById(id);
    }


    // 查询全部
    @GetMapping
    public List<Dictionarydetail> findAll(){
        return dictionarydetailService.loadAll();
    }


    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            dictionarydetailService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    // 添加和修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
        try {
            if (dictionarydetail.getId() == null){
                dictionarydetailService.save(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败啦");
        }
    }


    // 分页
    @PostMapping
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery dictionarydetailQuery){
        return dictionarydetailService.queryPage(dictionarydetailQuery);
    }



    // 批量删除
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            dictionarydetailService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");


        }
    }






}
